﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BikeSystem.Models;
using BikeSystem.BLL;
using System.Globalization;

namespace BikeUI
{
    public partial class FormBill : BikeUI.FormRental
    {

        private IList<RateOption> rateOptions;
        public FormBill()
        {
            InitializeComponent();


        }

        public IList<RateOption> RateOptions
        {
            set { 
                rateOptions = value;

                cbRateType.DataSource = rateOptions;
                cbRateType.DisplayMember = "RateType";
                cbRateType.ValueMember = "Rate";

            }
        }

        protected override void btnSave_Click(object sender, EventArgs e)
        {

            Rental rental = (Rental)rentalBindingSource.Current;
            rental.IsReturn = true;
            rental.ReturnTime = System.DateTime.Now;
            rental.UsedTime = rentedTimeTextBox.Text + " "+ cbRateType.Text;

            rental.Bill.Rate = double.Parse(rateTextBox.Text);
            rental.Bill.Amount = double.Parse(amountTextBox.Text);
            rental.Bill.RentedTime = double.Parse(rentedTimeTextBox.Text);


            CommonMessage msg = rentalManager.UpdateRental(rental);
            MessageBox.Show(msg.Message, msg.Caption);
            if (msg.Success)
            {
                this.Close();
            }
        }

        private void cbRateType_SelectedValueChanged(object sender, EventArgs e)
        {

            rateTextBox.Text = cbRateType.SelectedValue.ToString();
            
            Rental rental = (Rental)rentalBindingSource.Current;
            TimeSpan timeSpan = System.DateTime.Now.Subtract(rental.RentTime);
            if (ConstData.OPTION_BICYCLE_RATE_TYPE_HOURLY.Equals(cbRateType.Text))
            {
                rentedTimeTextBox.Text = timeSpan.Hours.ToString();
            }
            else if (ConstData.OPTION_BICYCLE_RATE_TYPE_DAILY.Equals(cbRateType.Text))
            {
                rentedTimeTextBox.Text = timeSpan.Days.ToString();
            }
        }

        private void rentedTimeTextBox_TextChanged(object sender, EventArgs e)
        {
            double unitTime = 0;
            if (double.TryParse(rentedTimeTextBox.Text, out unitTime))
            {
                double rate = 0;
                if (double.TryParse(rateTextBox.Text, out rate))
                {
                    amountTextBox.Text = (rate * unitTime).ToString("F2");
                }

                errorProvider1.SetError((TextBox)sender, "");
            }
            else {
                errorProvider1.SetError((TextBox)sender, "Invalid Number.");
            }
        }

        private void rateTextBox_TextChanged(object sender, EventArgs e)
        {
            double rate = 0;
            if (double.TryParse(rateTextBox.Text, out rate))
            {
                double rentedTime = 0;
                if (double.TryParse(rentedTimeTextBox.Text, out rentedTime))
                {

                    amountTextBox.Text = (rate * rentedTime).ToString("F2");

                    errorProvider1.SetError((TextBox)sender, "");
                }

                errorProvider1.SetError((TextBox)sender, "");
            }
            else
            {
                errorProvider1.SetError((TextBox)sender, "Invalid Number.");
            }
        }

        private void amountTextBox_TextChanged(object sender, EventArgs e)
        {
            double amount = 0;
            if (double.TryParse(amountTextBox.Text, out amount))
            {
                errorProvider1.SetError((TextBox)sender, "");
            }
            else
            {
                errorProvider1.SetError((TextBox)sender, "Invalid Number.");
            }
        }
    }
}
